package cn.virens.web.controller.manage.sys;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import cn.virens.common.RequestUtil;
import cn.virens.components.page.PageParamUser;
import cn.virens.components.page.PageResult;
import cn.virens.database.common.TransactionExecute;
import cn.virens.database.mysql.model.sys.user.SysUserRole;
import cn.virens.exception.APIException;
import cn.virens.service.inter.sys.user.SysUserRoleRelationService;
import cn.virens.service.inter.sys.user.SysUserRoleResourceService;
import cn.virens.service.inter.sys.user.SysUserRoleService;
import cn.virens.web.common.BaseTableController;

/**
 * 角色管理 - sys_user_role
 *
 * @author Virens
 * @since  2019-10-25
 */
@Controller
@RequestMapping("/manage/sys/role")
public class SysUserRoleContorller extends BaseTableController<SysUserRole, SysUserRoleService> {
	private @Autowired SysUserRoleRelationService mSysUserRoleRelationService;
	private @Autowired SysUserRoleResourceService mSysUserRoleResourceService;
	private @Autowired TransactionExecute mTransactionExecute;

	public SysUserRoleContorller() {
		super("/manage/sys/user_role", "sys:user:role");
	}

	@RequestMapping("view.jspx")
	public String viewJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("record", mBaseService.selectOne(RequestUtil.getValueInt(request, "id")));

		return returnUrl("view");
	}

	@RequestMapping("create.jspx")
	public String createJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		return returnUrl("create");
	}

	@RequestMapping("update.jspx")
	public String updateJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("record", mBaseService.selectOne(RequestUtil.getValueInt(request, "id")));

		return returnUrl("update");
	}

	@Override
	protected PageResult<?> listHandle(HttpServletRequest request, PageParamUser page) throws Exception {
		page.addParam("identity", RequestUtil.getValueStr(request, "identity", null));
		page.addParam("name", RequestUtil.getValueStr(request, "name", null));

		return super.listHandle(request, page);
	}

	@Override
	protected Model deleteHandle(HttpServletRequest request, Model model, String[] ids) throws Exception {
		return mTransactionExecute.write((s) -> {
			int asnwer = 0;

			for (String identity : ids) {
				asnwer += mBaseService.delete(identity);
			}

			if (asnwer <= 0 || asnwer != ids.length) {
				throw new APIException("ERROR", "删除失败");
			} else {
				return renderSuccess(model, "删除成功");
			}
		});
	}

	@ResponseBody
	@RequestMapping("relation/get.json")
	public Model relationGet(@RequestParam("uuid") String uuid, HttpServletRequest request, Model model) {
		return renderSuccess(mSysUserRoleRelationService.getRoleList(uuid), model);
	}

	@ResponseBody
	@RequestMapping("relation/put.json")
	public Model relationPut(@RequestParam("uuid") String uuid, @RequestBody JSONArray array, Model model) {
		return renderSuccess(mSysUserRoleRelationService.putRoleList(uuid, array), model);
	}

	@ResponseBody
	@RequestMapping("resource/get.json")
	public Model resourceGet(@RequestParam("identity") String identity, HttpServletRequest request, Model model) {
		return renderSuccess(mSysUserRoleResourceService.getResource(identity), model);
	}

	@ResponseBody
	@RequestMapping("resource/put.json")
	public Model resourcePut(@RequestParam("identity") String identity, @RequestBody JSONArray array, Model model) {
		return renderSuccess(mSysUserRoleResourceService.putResource(identity, array), model);
	}

	@Override
	protected Serializable toId(String id) {
		return id;
	}
}
